<template>
  <div class="lostAndFound">
    <div class="top_img" :style="'background-image: url(' + topBg + ')'">
      <span>失 物 招 领</span>
    </div>
    <div class="main">
      <div class="left">
        <div class="title">失物招领</div>
      </div>
      <div class="right">
        <div class="title">
          <span>
            <el-input
              placeholder="请输入内容"
              v-model="query.key.goodsName"
              clearable
            >
            </el-input>
            <el-button @click="getLostFoundList">搜索</el-button>
            <el-button @click="clikcUploadResources">添加失物招领</el-button>
          </span>
          <span>
            <span @click="$router.push('/')">首页</span>>><span>失物招领</span>
          </span>
        </div>

        <ul>
          <li
            v-for="(e, i) in initData"
            :key="i"
            @click="$router.push('/lost_and_found/details/' + e.id)"
          >
            <div class="col_img">
              <img :src="e.describedImgUrl" width="100%" alt="" />
            </div>
            <div class="col_title">
              {{ e.goodsName }}
            </div>
            <div class="col_comtent">
              {{ e.describedText }}
            </div>
            <div class="col_name_time">
              <span>
                {{ e.createdBy }}
              </span>
              <span>
                {{
                  $moment(parseInt(e.createdAt)).format("YYYY-MM-DD HH:mm:ss")
                }}
              </span>
            </div>
          </li>
          <li v-for="e in isList" :key="e + 200"></li>
        </ul>
      </div>
    </div>
    <el-pagination
      style="text-align: center; margin-top: 24px"
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="handleSizeChange"
      :page-size="query.pageSize"
    >
    </el-pagination>

    <el-dialog title="提示" :visible.sync="dialogVisible" width="768px">
      <div class="dialog_main">
        <div class="col_box">
          <div class="col_title">描述图片：</div>
          <el-upload
            class="upload-demo"
            drag
            :show-file-list="false"
            action="#"
            :http-request="imgUpload"
            :on-success="handleAvatarSuccess"
          >
            <div v-if="queryData.describedImgUrl == ''">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">点击上传</div>
            </div>
            <div v-if="queryData.describedImgUrl != ''">
              <img :src="queryData.describedImgUrl" alt="" width="100%" />
            </div>
          </el-upload>
        </div>
        <div class="col_box">
          <div class="col_title">物品名称：</div>
          <el-input
            v-model="queryData.goodsName"
            placeholder="请输入内容"
          ></el-input>
        </div>

        <div class="col_box">
          <div class="col_title">手机号码：</div>
          <el-input
            v-model="queryData.telephone"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">发布人:</div>
          <el-input
            v-model="queryData.createdBy"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">描述信息：</div>
          <el-input
            v-model="queryData.describedText"
            placeholder="请输入内容"
            type="textarea"
            :rows="8"
          ></el-input>
        </div>
      </div>

      <span slot="footer" class="dialog-footer">
        <el-button @click="clickCancel">取 消</el-button>
        <el-button type="primary" @click="clickSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  getLostFoundList,
  uploadImageLostFound,
  getAddLostFound,
} from "@/util/lostFound.js";

export default {
  data() {
    return {
      topBg:
        "http://175.178.92.128:8087/campus_help_system/images/insidePages/20190910163919740.jpg",
      initData: [],
      total: 6,
      query: {
        key: {
          goodsName: "",
          checked: 1,
        },
        pageSize: 6,
        pageNum: 1,
      },
      isList: 0,
      dialogVisible: false,
      queryData: {
        goodsName: "",
        describedText: "",
        userId: "",
        createdAt: "",
        createdBy: "",
        checked: 0,
        telephone: "",
        describedImg: "",
        describedImgUrl: "",
      },
    };
  },
  created() {
    this.getLostFoundList();
  },
  mounted() {},
  methods: {
    async getLostFoundList() {
      let { data: res } = await getLostFoundList(this.query);
      this.initData = res.data.list;
      this.total = res.data.total;
      if (this.initData.length <= 6) {
        this.isList = 6 - this.initData.length;
      } else {
        this.isList = 6 - (this.initData.length % 6);
      }
    },
    // 分页
    handleSizeChange() {
      this.query.pageNum = e;
      this.getLostFoundList();
    },

    // 上传图片
    async imgUpload(e) {
      var fm = new FormData();
      fm.append("coverFile", e.file);
      let { data: res } = await uploadImageLostFound(fm);
      console.log(res);
      this.queryData.describedImg = res.data.describedImg;
    },
    // 上传图片 成功的回调
    handleAvatarSuccess(res, file) {
      console.log(file);
      this.queryData.describedImgUrl = URL.createObjectURL(file.raw);
    },
    // 点击上传文件
    clikcDatumName() {
      window.open(this.queryData.describedImgUrl, "_blank");
    },

    // 点击按钮 上传资源
    clikcUploadResources() {
      if (
        window.localStorage.getItem("id") == "" ||
        window.localStorage.getItem("id") == null
      ) {
        return this.$message.error("请先登录");
      }
      this.dialogVisible = true;
    },

    // 点击按钮保存
    async clickSave() {
      console.log(this.queryData);
      // 判空
      if (this.isAddData()) {
        return;
      }
      this.queryData.checked = 0;
      this.queryData.userId = window.localStorage.getItem("id");

      let { data: res } = await getAddLostFound(this.queryData);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.clickCancel();
    },
    clickCancel() {
      this.queryData.describedImgUrl = "";
      this.queryData.describedImg = "";
      this.queryData.goodsName = "";
      this.queryData.telephone = "";
      this.queryData.createdBy = "";
      this.queryData.describedText = "";
      this.dialogVisible = false;
    },
    //判空
    isAddData() {
      if (
        this.queryData.describedImg == "" ||
        this.queryData.describedImg == null
      ) {
        this.$message.error("请输入描述图片");
        return true;
      }
      if (this.queryData.goodsName == "" || this.queryData.goodsName == null) {
        this.$message.error("请输入物品名称");
        return true;
      }

      if (this.queryData.telephone == "" || this.queryData.telephone == null) {
        this.$message.error("请输入手机号码");
        return true;
      }

      if (this.queryData.createdBy == "" || this.queryData.createdBy == null) {
        this.$message.error("请输入发布人");
        return true;
      }

      if (
        this.queryData.describedText == "" ||
        this.queryData.describedText == null
      ) {
        this.$message.error("请输入描述");
        return true;
      }
      return false;
    },
  },
};
</script>
<style lang="less" scoped>
.lostAndFound {
  padding-bottom: 50px;
  .top_img {
    height: 280px;
    background-repeat: no-repeat;
    background-size: auto 100%;
    background-position: center center;
    display: flex;
    align-items: center;
    justify-content: center;
    & > span {
      font-size: 48px;
      font-weight: 700;
      color: #fff;
    }
  }
  .main {
    width: 1200px;
    margin: 0 auto;
    padding-top: 50px;
    display: flex;

    .left {
      margin-right: 60px;
      .title {
        height: 50px;
        font-size: 18px;
        background: #0166b6;
        padding-left: 15px;
        width: 200px;
        line-height: 50px;
        color: #fff;
      }
    }
    .right {
      flex: 1;
      .title {
        justify-content: space-between;
        display: flex;
        padding: 0 0 12px 0;
        border-bottom: 1px solid #ddd;
        & > span:nth-child(1) {
          display: flex;
          .el-button {
            margin-left: 12px;
          }
        }
        & > span:nth-child(2) {
          & > span:nth-child(1) {
            cursor: pointer;
            margin-right: 12px;
          }
          & > span:nth-child(2) {
            color: #0e3782;
            margin-left: 12px;
          }
        }
      }

      & > ul {
        padding: 20px 0;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        justify-content: space-between;
        & > li {
          margin-bottom: 24px;
          cursor: pointer;
          width: calc(33.33% - 12px);
          .col_img {
            margin-bottom: 12px;
            height: 302px;
            overflow: hidden;
          }
          .col_title {
            margin-bottom: 12px;
            font-size: 16px;
          }
          .col_comtent {
            height: 65px;
            line-height: 1.5;
            font-size: 14px;
            margin-bottom: 8px;
            text-overflow: -o-ellipsis-lastline;
            overflow: hidden;
            text-overflow: ellipsis;
            display: -webkit-box;
            -webkit-line-clamp: 3;
            line-clamp: 3;
            -webkit-box-orient: vertical;
          }
          .col_name_time {
            display: flex;
            justify-content: space-between;
            align-items: center;
            span {
              color: #aaa;
              font-size: 14px;
              margin-right: 12px;
            }
          }
        }
      }
    }
  }
  .dialog_main {
    .col_box {
      display: flex;
      align-items: center;
      margin-bottom: 24px;
      .col_title {
        width: 120px;
        text-align: right;
        margin-right: 8px;
      }
      .el-input,
      .el-textarea {
        width: 560px;
      }
      /deep/ .el-upload-dragger {
        width: 340px;
        height: 170px;
      }
      .upload-demo {
        margin-bottom: 0.8rem;
      }
    }
  }
}
</style>
